#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxCol = 1005;
const int maxRow = 105;
int M,N;
bool isFirst;

bool isMinHeap(int tree[],int root){
    if(root*2>N) return true;
    else if(root*2+1 > N) {
        if(tree[root]<tree[root*2]) return true;
    }
    else {
        int lChild = root*2,rChild = root*2+1;
        if(tree[root]<tree[lChild]&&
            tree[root]<tree[rChild]&&
            isMinHeap(tree,lChild)&&
            isMinHeap(tree,rChild)) return true; 
    }
    return false;
}
bool isMaxHeap(int tree[],int root){
    if(root*2>N) return true;
    else if(root*2+1 > N) {
        if(tree[root]>tree[root*2]) return true;
    }
    else {
        int lChild = root*2,rChild = root*2+1;
        if(tree[root]>tree[lChild]&&
            tree[root]>tree[rChild]&&
            isMaxHeap(tree,lChild)&&
            isMaxHeap(tree,rChild)) return true; 
    }
    return false;
}
void postTraverse(int tree[],int root) {
    int lChild = root*2,rChild = root*2+1;
    if(lChild<=N) postTraverse(tree,lChild);
    if(rChild<=N) postTraverse(tree,rChild);
    if(isFirst) {
        printf("%d",tree[root]);
        isFirst = false;
    }
    else printf(" %d",tree[root]);
}
int main(void)
{
    cin>>M>>N;
    int trees[maxRow][maxCol];
    fill(trees[0],trees[0]+maxRow*maxCol,0);
    for(int i=0;i<M;i++) {
        isFirst = true;
        for(int j = 1; j <= N; j++) {
            cin>>trees[i][j];
        }
        if(isMinHeap(trees[i],1)) {
            printf("Min Heap\n");   
        } else if(isMaxHeap(trees[i],1)) {
            printf("Max Heap\n");
        } else {
            printf("Not Heap\n");
        }
        postTraverse(trees[i],1);
        printf("\n");
    }

}